package com.berje.vkalarm.vksdk.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GroupAudioNewsItem {
	private List<Audio> audios = new ArrayList<Audio>();

	private long date;

	public static GroupAudioNewsItem parse(JSONObject jitem)
			throws JSONException {
		GroupAudioNewsItem ret = new GroupAudioNewsItem();
		ret.setDate(jitem.optLong("date", 0));
		JSONArray attachments = jitem.optJSONArray("attachments");
		if (attachments != null) {
			int size = attachments.length();
			for (int j = 0; j < size; ++j) {
				Object att = attachments.get(j);
				if (att instanceof JSONObject == false)
					continue;
				JSONObject json_attachment = (JSONObject) att;
				String type = json_attachment.getString("type");

				if ("audio".equals(type)) {
					Audio audio = Audio.parse(json_attachment
							.getJSONObject("audio"));
					if (audio != null) {
						ret.addAudio(audio);
					}
				}

			}
		}
		if (ret.getAudios().size() == 0)
			return null;

		return ret;
	}

	public List<Audio> getAudios() {
		return audios;
	}

	public void setAudios(List<Audio> audios) {
		this.audios = audios;
	}

	public void addAudio(Audio audio) {
		this.audios.add(audio);
	}

	public long getDate() {
		return date;
	}

	public void setDate(long date) {
		this.date = date;
	}

	public String toString() {
		return String.format("GroupMusicNewsItem{audios: %s}", audios);
	}

}
